<?
class ProductController extends BaseController{
    var $table = 'products';
    var $id_field = 'pro_id';
    var $name_field = 'pro_name';
    var $thumb_field = 'pro_thumb';
    var $slide_field = 'pro_slide';
    var $record_id;
    var $factory = array();
    public function __construct($id = 0){
        parent::__construct($id);
        if($id) {
            $pro = $this->getDataField();
            $fac_name = $this->GetFactory();
            $fac_name = $fac_name['fac_name'];
            $link = generate_product_detail_url(array('pro_id'=>$pro['pro_id'],'pro_name'=>$pro['pro_name'],'fac_name'=>$fac_name));
            $seo_mt = showSeoMeta('products',$id);
                if($seo_mt != '') {
                    $im_seo = $seo_mt;
                }else {
                    $im_seo = '<title>'.$pro['pro_name'].'</title>';
                    $im_seo .= '<meta name="description" content="'.cut_string(removeHTML($pro['pro_basic_info']),150).'" />';
            }
            $this->seo_string = ''.$im_seo.'
                                 <link rel="canonical" href="http://fonecare.vn'.$link.'" />
                                 <meta property="og:site_name" content="'.$pro['pro_name'].'" />
    	                         <meta property="og:image" content="http://fonecare.vn/pictures/product/'.$pro['pro_thumb'].'" />
                            	 <meta property="og:type" content="news" />
                            	 <meta property="og:url" content="http://fonecare.vn'.$link.'" />
                            	 <meta property="og:title" content="'.$pro['pro_name'].'" />
                            	 <meta property="fb:app_id" content="524593015322765" />
                            	 <meta property="fb:admins" content="100000472324543" />';
        }else {
            $this->seo_string = '<title>Fone Care - Sản phẩm</title>
                                 <meta name="description" content="Sản phẩm điện thoại, linh phụ kiện điện thoại, dịch vụ điện thoại" />
                                 <meta name="keywords" content="điện thoại, phụ kiện điện thoại, dịch vụ điện thoại" />
                                 <meta property="og:site_name" content="Fone Care - Sản phẩm" />
    	                         <meta property="og:image" content="http://fonecare.vn/template/default/themes/img/logo.png" />
                            	 <meta property="og:type" content="news" />
                            	 <meta property="og:url" content="http://fonecare.vn/san-pham" />
                            	 <meta property="og:title" content="Fone Care - Sản phẩm" />
                            	 <meta property="fb:app_id" content="524593015322765" />
                            	 <meta property="fb:admins" content="100000472324543" />';   
        }
    }

    /*
     * Hàm get -> alias của hàm getDataField
     */
    public function get($field_name){
        return $this->getDataField($field_name);
    }
    public function GetBreadcrumb(){
        $factory = $this->GetFactory();
        $link = generate_factory_url($factory);
        $attribute = array('link'=>$link, 'factory'=>$factory['fac_name']);
        return ProductLayout::generate_detail_breadcrumb($attribute);
    }
    public function GetFactory(){
        if($this->factory) return $this->factory;
        $fac_id = $this->getDataField('pro_factory');
        $fac = new FactoryController($fac_id);
        $this->factory = $fac->getDataField('fac_id,fac_name');
        return $this->factory;
    }
    public function GetFactoryName(){
        return $this->factory['fac_name'];
    }
    public function GetProductThumb(){
        return PATH_PICTURE_PRODUCT . $this->GetThumb();
    }

    //Ham nay nhan vao 1 array info chua cac gia tri thuoc tinh cua san pham
    //Tra ve client theo format trong Layout
    public function GetProductSpec($array_spec){
        global $global_product_spec;
        $spec_field_array = array();
        //Gan gia tri vao mang ket qua
        foreach($global_product_spec as $group){
            $spec_field['label'] = $group['label'];
            $spec_field['info'] = array();
            foreach($group['info'] as $spec){
                $temp = array();
                $temp['field'] = $spec['text'];
                $temp['value'] = $array_spec[$spec['spec']];
                $spec_field['info'][] = $temp;
            }
            $spec_field_array[] = $spec_field;
        }
        //tra ve gia tri html cho client
        $html = ProductLayout::product_spec_detail($spec_field_array);
        return $html;
    }

    //hàm trả về bài viết của
    public function GetProductDetail(){

    }
    //Ham lay vidoe tu youtube roi tra ve trang cua minh
    public function GetYoutubeVideo($video_url){
        //lay id
        if(!$video_url){
            return '';
        }
        $arr_temp = explode(';',$video_url);
        $list_video = array();
        foreach($arr_temp as $tmp){
            $list_video[] = getYouTubeIdFromURL($tmp);
        }
        $html = ProductLayout::product_video_detail($list_video);
        return $html;
    }

    public function GetProductDetailSlide(){
        if(!$this->slides){
            return '';
        }
        $slides = explode('|',$this->slides);
        $tmp = array();
        foreach($slides as $v){
            $tmp[] = PATH_PICTURE_PRODUCT . $v;
        }
        return ProductLayout::product_slide_detail(array('slides'=>$tmp));
    }
    /*
     * Lấy các phụ kiện liên quan
     */
    public function getProductCombo(){
        global $global_product_status_in_stock;
        $combo_string = $this->get('pro_combo');
        if(!$combo_string){
            return '';
        }
        $combo_array = array();
        $db_query = new db_query('SELECT pro_id, pro_name,pro_thumb,pro_price,pro_status_in_stock,pro_number_in_stock
                                  FROM products
                                  WHERE pro_id IN('.$combo_string.')');
        while($row = mysql_fetch_assoc($db_query->result)){
            $tmp = array(
                'thumb'=> PATH_PICTURE_PRODUCT . $row['pro_thumb'],
                'name'=> $row['pro_name'],
                'price'=> ($row['pro_price'] == 68000000 ? 'Liên hệ' : format_price($row['pro_price'])),
                'link'=> generate_product_detail_url($row),
                'status'=> $global_product_status_in_stock[$row['pro_status_in_stock']]
                . ($row['pro_status_in_stock'] ? '('.$row['pro_number_in_stock'].')' : '')
            );
            $combo_array[] = $tmp;
        }
        return ProductLayout::product_combo_detail($combo_array);
    }
    /*
     * Lấy các sản phẩm liên quan
     */
    public function getProductRelation(){
        global $global_product_status_in_stock;
        $pro_relate = $this->get('pro_relate');
        if(!$pro_relate){
            return '';
        }
        $pro_array = array();
        $db_query = new db_query('SELECT pro_id, pro_name,pro_thumb,
                                        pro_price,pro_status_in_stock,
                                        pro_number_in_stock,fac_name,fac_id
                                  FROM products
                                  LEFT JOIN factory ON fac_id = pro_factory
                                  WHERE pro_id IN('.$pro_relate.')');
        while($row = mysql_fetch_assoc($db_query->result)){
            $tmp = array(
                'thumb'=> PATH_PICTURE_PRODUCT . $row['pro_thumb'],
                'name'=> $row['pro_name'],
                'price'=> ($row['pro_price'] == 68000000 ? 'Liên hệ' : format_price($row['pro_price'])),
                'link'=> generate_product_detail_url($row),
                'status'=> $global_product_status_in_stock[$row['pro_status_in_stock']]
            );
            $pro_array[] = $tmp;
        }
        return ProductLayout::product_relate_detail($pro_array);
    }
}
?>